//main.c
#include <reg51.h>
#include "lcd12864.h"
sbit DRV = P0^4;//对应实验箱IO5
unsigned char circle=0,current_speed_val=0,target_speed_val=30,flag_1s=0,counter=0,pwm_val=50;   //counter计数，0-100。pwm_val指的是占空比（脉冲宽度调整）pwm_val 最大值为100。总份数在本程序中亦为100
char current_speed[4];
char target_speed[4];
char pwm[4];
 
unsigned char code img[][16] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x04,0x02,0x55,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x3B,0xB4,0x00,0x00,
0x00,0x00,0x04,0x0B,0xEA,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x00,0xAA,0xA8,
0x00,0x00,0x12,0x24,0x29,0xA3,0x00,0x00,0x00,0x00,0x0A,0x00,0x2C,0x00,0x10,0x04,
0x00,0x01,0x55,0x37,0x84,0x3B,0x00,0x00,0x00,0x00,0x42,0x20,0x26,0x80,0x0A,0x42,
0x00,0x02,0x00,0x80,0x04,0x4A,0x80,0x00,0x00,0x0A,0x99,0x10,0x01,0x78,0x00,0x10,
0x00,0x28,0xAF,0xDA,0x08,0x32,0x00,0x02,0x00,0x2D,0x62,0xC0,0x00,0xD7,0x80,0x08,
0x2C,0x95,0xB0,0xC0,0x05,0x6E,0x00,0x03,0x40,0xA2,0xBD,0x74,0x00,0x12,0x80,0x00,
0x02,0x41,0x54,0x14,0x84,0x26,0x00,0x01,0x2E,0xBA,0xAD,0x2E,0x80,0x5A,0xC0,0x00,
0x00,0x24,0xC0,0x0A,0x41,0x33,0xD0,0x00,0x13,0x46,0x95,0xB3,0x6D,0x56,0x80,0x00,
0x00,0x1B,0x00,0x00,0x52,0x9C,0x6A,0x00,0x4D,0x58,0x01,0x6C,0xAA,0xE9,0x00,0x00,
0x0A,0x80,0x00,0x00,0x29,0x4B,0x2D,0x56,0xA5,0x70,0x00,0x0A,0xAD,0x2A,0x00,0x00,
0x50,0x00,0x00,0x00,0x00,0x29,0xAA,0xAA,0xA5,0x00,0x00,0x60,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x84,0xDD,0x55,0x58,0x01,0x55,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x28,0x01,0x4A,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
 
 
 
void main(void){
	IE=0x81;//EA 空 空 ES ET1 EX1 ET0 EX0
	TCON=0x01;//TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0(IT0=1表示外部中断0位边沿触发)
    TMOD=0x11;//计时器0和计时器1均为方式1
	TL1 = 0xA4;		//单片机CPU主频11.0892MHz，1/12是0.9241M
    TH1 = 0xFF;		//(65536-初值）/(0.9241M)=0.1ms,得到初值是65444=0xFFA4
    TH0=0x4b;
	TL0=0x83;//   (65536-19331)/(0.9241M)=0.05s,19331=0x4b83 ,20次中断为1秒
	lcd_init();
	lcd_clear_txt();
	lcd_display_picture(img);
	while(1){
	  target_speed_val=P2;//将8个开关连接至P2，最左侧开关接P2.7,最右侧开关接P2.0，可以表示0-255范围的数
	  target_speed[2]='0'+target_speed_val%10; //个位
	  target_speed[1]='0'+(target_speed_val/10)%10; //十位
	  target_speed[0]='0'+(target_speed_val/100)%10; //百位
	  target_speed[3]='\0';   //无
 
	  put_str(0,0,"占空比");
	  put_str(0,6,pwm);
      put_str(1,0,"目标转速");
      put_str(1,6,target_speed);
	  put_str(2,0,"当前转速");
	  put_str(2,6,current_speed);
 
      ET0=1;
	  TR0=1;
	  ET1=1;
   	  TR1=1;
	}
}
 
 
 
/****************************************************************
定时器T1实现pwm
****************************************************************/
void timer1_interrupt() interrupt 3   //每隔100us进来一次
{
	TL1 = 0xA4;		//单片机CPU主频11.0892MHz，1/12是0.9241M
    TH1 = 0xFF;		//(65536-初值）/(0.9241M)=0.1ms,得到初值是65444=0xFFA4
    //counter=(counter+1)%100;
	counter+=pwm_val;
	if(counter>=100) {
		DRV=1;
		counter-=100;
	}
	else{
		DRV=0;
	}

	if(counter<pwm_val)
	{
		DRV=0;//实验箱的电机低电平运行
	}
	else    //50低 50高
	{
		DRV=1;
	}
	ET1=1;
    TR1=1;
}
 
/****************************************************************
定时器T0实现每秒更新转速，并根据当前转速调整pwm
****************************************************************/
void timer0_interrupt() interrupt 1{
 
	EA=0;
	TH0=0x4b;
	TL0=0x83;    //计时50ms
	flag_1s++;
    if(flag_1s==20){
	  current_speed_val=circle;
	  current_speed[2]='0'+current_speed_val%10;
	  current_speed[1]='0'+(current_speed_val/10)%10;
      current_speed[0]='0'+(current_speed_val/100)%10;
	  current_speed[3]='\0';//得到在刚刚过去的一秒内的圈数，也就是当前转速
      circle=0;
 
      //根据当前转速和目标转速调整占空比
      if(current_speed_val>target_speed_val){
        if(pwm_val>0)pwm_val--;
      }else{
        if(pwm_val<99)pwm_val++;
      }
      pwm[1]='0'+pwm_val%10;
	  pwm[0]='0'+(pwm_val/10)%10;
	  pwm[2]='\0';
 
      flag_1s=0;
	}
 
    ET0=1;
    TR0=1;
	EA=1;
}
 
/****************************************************************
外部中断0,脉冲计数记录电机的转速,电机转一圈circle加一
****************************************************************/
void int0_interrupt() interrupt 0{
     circle++;
	 IE0=0;
}
